package com.optionexplorer.strategies;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.json.simple.JSONObject;

import com.optionexplorer.data.CallOption;
import com.optionexplorer.data.PutOption;
import com.optionexplorer.data.StockOption;
import com.optionexplorer.util.DateUtils;
import com.optionexplorer.util.NumberUtils;

public class ShortCallShortPutPackage {

	private String _ticker = null;
	private float _stockPrice;
	private int _quantity;
	
	private CallOption _callObj = null;
	private PutOption _putObj = null;
		
	private float _retOnExerciseAmt;
	private float _retOnExercisePct;
	private float _netPriceOnExercise;
	private float _netDiscOnExercise;
	private float _annualizedReturn;
	
	
	public void setStock(String str, 
						 float price, 
						 int qty) {
		this._ticker = str;
		this._stockPrice = price;
		this._quantity = qty;
		return;
	}
		
	public void addCall(CallOption c) {
		this._callObj = c;
		return;
	}
	
	public void addPut(PutOption p) {
		this._putObj = p;
		return;
	}
	
	
	public void computeReturns() {
		
		float stockReturn = 0;
		float callReturn = 0;
		float putReturn = 0;
		float capitalInvested = 0;
		float capitalForMargin = 0;
		int daysRemaining = 0;
		
		stockReturn = this._quantity * (this._callObj.getStrikePrice() - this._stockPrice);
		callReturn = this._quantity * (this._callObj.getPremium());
		putReturn = this._quantity * (this._putObj.getPremium());
		capitalInvested = (this._quantity * this._stockPrice) - callReturn - putReturn;
		capitalForMargin = this._quantity * this._putObj.getStrikePrice();
		
		this._retOnExerciseAmt = NumberUtils.roundTwoDecimals(stockReturn + callReturn + putReturn);
		this._retOnExercisePct = (this._retOnExerciseAmt) / (capitalInvested + capitalForMargin);
		this._retOnExercisePct = NumberUtils.roundTwoDecimals(this._retOnExercisePct * 100);
		
		Date d = this._callObj.getExpirationDate();
		daysRemaining = DateUtils.getDaysRemaining(d);
		
		this._annualizedReturn = NumberUtils.roundTwoDecimals((this._retOnExercisePct * 365) / daysRemaining);
		
		//System.out.println("Ret on Exercise : " + this._retOnExercisePct);
		//System.out.println("Days remaining : " + daysRemaining);
		//System.out.println("Annual Return : " + this._annualizedReturn);
		
		this._netPriceOnExercise = ((this._stockPrice - this._callObj.getPremium()) + 
				(this._putObj.getStrikePrice() - this._putObj.getPremium()))/2;
		
		
		return;
	}
	
	
	public String toString() {
		
		StringBuilder sb = new StringBuilder();
		sb.append(this._ticker + "\t");
		sb.append(this._callObj.getSymbol() + "\t");
		sb.append(this._putObj.getSymbol() + "\t");
		sb.append(this._retOnExerciseAmt + "\t");
		sb.append(this._retOnExercisePct + "\t");
		sb.append(this._annualizedReturn + "\t");
		sb.append(this._netPriceOnExercise + "\n");
		
		
		return sb.toString();
	}
	
	public JSONObject toJSON() {
		
		JSONObject obj = new JSONObject();
		obj.put("Ticker", this._ticker);
		obj.put("Call",this._callObj.getSymbol());
		obj.put("Put", this._putObj.getSymbol());
		obj.put("AmtReturn", this._retOnExerciseAmt);
		obj.put("PercentReturn",this._retOnExercisePct);
		obj.put("AnnualReturn",this._annualizedReturn);
		obj.put("PriceOnAssign", this._netPriceOnExercise);
		
		return obj;
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ShortCallShortPutPackage pkg = new ShortCallShortPutPackage();
		float price = (float) 50.14;
		float callPremium = (float) 5.2;
		float putPremium = (float) 4.5;
		float putStrike = (float) 47.5;
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, 2012);
		cal.set(Calendar.MONTH, 1);
		cal.set(Calendar.DAY_OF_MONTH, 21);
		Date expDate = cal.getTime();
		pkg.setStock("WMT", price, 100);
		
		CallOption c = new CallOption(price, 
									  "WMT120121C00050000", 
									  expDate, 
									  50, 
									  callPremium, 
									  100, 
									  1000);
		PutOption p = new PutOption(price, 
				  "WMT120121P00050000", 
				  expDate, 
				  putStrike, 
				  putPremium, 
				  100, 
				  1000);		

		pkg.addCall(c);
		pkg.addPut(p);
		pkg.computeReturns();
		
		System.out.println(pkg);
		
	}
	
}
